from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from controller.NoticeAdminController import NoticeAdminController
from controller.PublicAdminController import PublicAdminController
from schema.QuerySchema import NoticeQuery, QueryBase
from schema.TeachingSchema import NoticeInfo
from util.Auth import check_token
from util.DataBaseTools import get_db_session
from util.ErrorCode import ErrorCode
from util.LogHelper import Log

notice_admin_router = APIRouter()


@notice_admin_router.post("/notice/send", summary='添加通知信息并发送')
def send_notice(noticeInfo: NoticeInfo, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/notice/send')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/notice/send', db):
            noticeInfo.user_id = result.get('data').user_id
            response = NoticeAdminController.send_notice(noticeInfo, db)
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
        return response
    else:
        return result


@notice_admin_router.post("/notice/send/again/{notice_id}", summary='根据通知信息id重新发送邮件')
def resend_notice(notice_id: int, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/notice/send/again')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/notice/send', db):
            response = NoticeAdminController.resend_notice(notice_id, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@notice_admin_router.post("/notice/querybypage", summary='分页查询所有通知信息')
def query_notice_by_page(queyr_base: QueryBase, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/notice/querybypage')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/notice/query', db):
            response = NoticeAdminController.query_notice_by_page(queyr_base, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result


@notice_admin_router.post("/notice/query", summary='根据创建人id查询通知信息')
def query_notice_by_field(noticeQuery: NoticeQuery, db: Session = Depends(get_db_session), result=Depends(check_token)):
    Log.getlogger().debug(f'recieved request:/notice/query')
    if result.get('code') == 0:
        if PublicAdminController.has_permission(result.get('data').port_id, '/notice/query', db):
            response = NoticeAdminController.query_notice_by_field(noticeQuery, db)
            return response
        else:
            return {'code': ErrorCode.USERID_HAS_NO_PERMISSION.value, 'message': 'user has no permission'}
    else:
        return result
